/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;
import bean.Usuario;
import java.awt.event.KeyListener;
import java.util.ResourceBundle;
import javax.swing.JOptionPane;
import main.*;

/**
 *
 * @author yulian
 */
public class PanelLogin extends javax.swing.JPanel {
    
    private ProgBarHandler pgh ;
   
    public ProgBarHandler getPGB(){return pgh;}
    public Object getProgBar(){
        return this.barraProg;
    }    
    public PanelLogin() {
        initComponents();
        
    }
     
     String idUsuario;
     String n;
     int out;
    
    
    public String getIdUsuario(){
        return idUsuario;
    }
    public String getN(){
        return n;
    }
    public int getOut(){
        return out;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        Ingresar = new javax.swing.JButton();
        ChangePass = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtUser = new javax.swing.JTextField();
        barraProg = new javax.swing.JProgressBar();
        txtPass = new javax.swing.JPasswordField();
        jLabel1 = new javax.swing.JLabel();

        setLayout(null);

        Ingresar.setText("Ingresar");
        Ingresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                IngresarActionPerformed(evt);
            }
        });
        Ingresar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                IngresarKeyPressed(evt);
            }
        });
        add(Ingresar);
        Ingresar.setBounds(80, 400, 130, 20);

        ChangePass.setText("Cambiar Clave");
        ChangePass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ChangePassActionPerformed(evt);
            }
        });
        add(ChangePass);
        ChangePass.setBounds(80, 450, 130, 20);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 51, 51));
        jLabel2.setText("Usuario:");
        add(jLabel2);
        jLabel2.setBounds(80, 140, 80, 10);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(0, 51, 51));
        jLabel3.setText("Contraseña:");
        add(jLabel3);
        jLabel3.setBounds(80, 200, 90, 10);
        add(txtUser);
        txtUser.setBounds(80, 160, 140, 20);

        barraProg.setStringPainted(true);
        add(barraProg);
        barraProg.setBounds(30, 500, 220, 20);

        txtPass.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPassActionPerformed(evt);
            }
        });
        txtPass.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtPassKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtPassKeyReleased(evt);
            }
        });
        add(txtPass);
        txtPass.setBounds(80, 220, 140, 20);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/views/fondologin.JPG"))); // NOI18N
        add(jLabel1);
        jLabel1.setBounds(-440, -40, 720, 620);
    }// </editor-fold>//GEN-END:initComponents

    private void BarLoad(){
           
        this.pgh = new ProgBarHandler(main.Main.log.getPanelLogin1().getProgBar());
        try{
            this.pgh.start();// Llama a run()
        }catch(Exception e){e.printStackTrace();}
        try{
            this.pgh.setOrder(1);
        }catch(Exception e){e.printStackTrace();}
    }
    private void IngresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_IngresarActionPerformed
         
          BarLoad();
          idUsuario =txtUser.getText();
          n =new String (txtPass.getPassword());           
          out =Main.service.queryIdPass(idUsuario, n);

    }//GEN-LAST:event_IngresarActionPerformed

    
    private void txtPassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPassActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPassActionPerformed

    private void txtPassKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPassKeyReleased
        
//        if(evt.getKeyCode()==13){
//          BarLoad();
//          idUsuario =txtUser.getText();
//          n =new String (txtPass.getPassword());           
//          out =Main.service.queryIdPass(idUsuario, n);
//        }

    }//GEN-LAST:event_txtPassKeyReleased

    private void txtPassKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtPassKeyPressed
         
//         pass.addKeyListener(new KeyListener(){
//            @Override
//            public void keyTyped(KeyEvent e) {
//                if(e.getKeyChar()==KeyEvent.VK_ENTER){
//                    OKButton.doClick();
//                }
//            }
    }//GEN-LAST:event_txtPassKeyPressed

    private void ChangePassActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ChangePassActionPerformed
        
        idUsuario =txtUser.getText();
        n =new String (txtPass.getPassword());       
        out =Main.service.queryIdPass(idUsuario, n);
        if(out==1){
            Main.dialog.setVisible(true);
        }
        else{
        JOptionPane.showMessageDialog(null,"      Usuario o contraseña incorrectos!","Usuario",JOptionPane.INFORMATION_MESSAGE);
        }
    }//GEN-LAST:event_ChangePassActionPerformed

    private void IngresarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_IngresarKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_IngresarKeyPressed
   
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ChangePass;
    private javax.swing.JButton Ingresar;
    private javax.swing.JProgressBar barraProg;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPasswordField txtPass;
    private javax.swing.JTextField txtUser;
    // End of variables declaration//GEN-END:variables
}
